package com.linkai.基础._01_copy;

import com.linkai.JVM.Student;

/**
 * @Description: 浅拷贝
 *               被复制对象的所有变量都含有与原来的对象相同的值，而所有的对其他对象的引用仍然指向原来的对象。
 *               即对象的浅拷贝会对“主”对象进行拷贝，但不会复制主对象里面的对象。”里面的对象“会在原来的对象和它的副本之间共享。
 * @Author: 林凯
 * @Date: 2021/10/29 23:21
 */
public class _03_ShallowCopy {
    public static void main(String[] args) throws CloneNotSupportedException {
        Teacher3 t1 = new Teacher3("JackLin", 21);
        Student3 s1 = new Student3("s_jacklin", 18, t1);
        Student3 s2 = (Student3) s1.clone();

        // 浅拷贝会对“主”对象进行拷贝，但不会复制主对象里面的对象
        // 所以 s1 是 s2 不同的对象，而里面的 teacher 对象还是同一个对象
        System.out.println(s1);
        System.out.println(s2);
        System.out.println(s1.getTeacher());
        System.out.println(s2.getTeacher());
        /*
            com.linkai.基础._01_copy.Student3@74a14482
            com.linkai.基础._01_copy.Student3@1540e19d
            com.linkai.基础._01_copy.Teacher3@677327b6
            com.linkai.基础._01_copy.Teacher3@677327b6
        * */
    }
}

class Teacher3 implements Cloneable {
    private String name;
    private int age;

    public Teacher3(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

class Student3 implements Cloneable {
    private String name;
    private int age;
    private Teacher3 teacher;

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }

    public Student3(String name, int age, Teacher3 teacher) {
        this.name = name;
        this.age = age;
        this.teacher = teacher;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public Teacher3 getTeacher() {
        return teacher;
    }

    public void setTeacher(Teacher3 teacher) {
        this.teacher = teacher;
    }


}
